import ConfigParser
from matplotlib import transforms 
from Converters import UnitConverter, ScaleConverter

class DissociationLimit(object):
    def __init__(self, label, value):
        self.label = label
        self.plotvalue = self.value = value
        
    def remove(self,  value):
        self.statictext.remove()
        self.line.remove()
            
    def set_visible(self,  flag):
        self.statictext.set_visible(flag)
        self.line.set_visible(flag)
        
    def plot(self,  axes,  draw=False):
        self.line = axes.axhline(y=self.plotvalue, color='gray', ls='-.')
        
        #We want the y coords of the labels to move with data while
        #the x coords stay fixed        
        fig = axes.get_figure()        
        trans = transforms.blended_transform_factory(fig.transFigure, axes.transData)
        x = fig.subplotpars.right + .01  

        self.statictext = axes.text(x, self.plotvalue, self.label,  transform=trans, size='small')
        if draw:
            axes.draw_artist(self.statictext)
            axes.draw_artist(self.line)

class DissociationLimits(object):
    uConv = UnitConverter() 
    uConv.buildConversionTable()
           
    def __init__(self, molname):   
        self.readMolData( molname)
        
    def readMolData(self,  molname):
        limitsdict = {}
        self.Limits = []
        filename = 'molecules.cfg'
        parser = ConfigParser.ConfigParser()
        parser.optionxform = str
        parser.read(filename)  
        
        options = parser.options(molname)
        self.ground_state = parser.getfloat(molname, options.pop(options.index('Ground State')))
        self.scale = parser.get(molname, options.pop(options.index('Scale')))
        self.unit = parser.get(molname, options.pop(options.index('Units')))
        self.SC = ScaleConverter(self.ground_state)
        
        for key in options:
            val = limitsdict[key] = parser.getfloat(molname, key)       
            add = DissociationLimit(key,  val)
            self.Limits.append(add)
    
    def draw(self, axes, outunits=None):
        for limit in self.Limits:
            if outunits:
                limit.plotvalue = limit.value + self.SC.conversion_offset(self.scale,  outunits['Scale'])
                limit.plotvalue *= DissociationLimits.uConv.conversion_factor('Energy', self.unit, outunits['Energy'])
            limit.plot(axes)
    
    def update(self,  axes):
        ymin, ymax = axes.get_ylim()
        xmin, xmax = axes.get_xlim()
        for limit in self.Limits:
            if limit.plotvalue > ymax or limit.plotvalue < ymin:
                limit.set_visible(False)
            else: 
                limit.set_visible(True)


import wx
from matplotlib.figure import Figure
from PlotPanel2 import  PlotPanel

class DemoPanel(PlotPanel):
    def makeFig(self,  Fig):
        PlotPanel.makeFig(self, None)        
        self.diss = DissociationLimits('Rb2')                 
        self.ax.set_xlim(0,20)
        self.ax.set_ylim(16000,26000)
        win = [0,20, -5000, 30000]
        ounits = {'Energy': 'wavenumbers', 'Scale': 'molecular'}
        self.diss.draw(self.ax, ounits)
        self.diss.update(self.ax)
        self.initProps(win)        
        for limit in self.diss.Limits:
            print limit.label + ' ' + str(limit.value) + ' ' + str(limit.plotvalue) 
    
    def updateWindow(self):
        PlotPanel.updateWindow(self)
        self.diss.update(self.ax)
    
    def resetAxes(self,  props):
        PlotPanel.resetAxes(self,  props)
        self.diss.update(self.ax)
      

if __name__ == "__main__":
    
    app = wx.PySimpleApp()
    frame = wx.Frame(None,-1,'PlotPanel')
    
    panel = DemoPanel(frame)
    panel.canvas.draw()
    frame.SetSize((800,600))
    frame.Show()
    app.MainLoop()
    

